package cn.whut.glab.web;

import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.sql.SQLException;
import java.util.List;

import com.google.gson.Gson;

import cn.whut.glab.domain.BD_Student;
import cn.whut.glab.domain.BD_Teacher;
import cn.whut.glab.domain.EasyUIPager;
import cn.whut.glab.service.StudentService;
/**
 * Servlet implementation class StudentListServlet
 */
public class StudentListServlet extends HttpServlet {
	StudentService service = new StudentService();

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		doPost(request, response);
		
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		
		response.setHeader("Content-type", "text/html;charset=UTF-8"); 
		request.setCharacterEncoding("utf-8");
		String method=request.getParameter("method");
		if(method==null){
			//page锛氬綋鍓嶉〉   rows锛氭瘡椤电殑璁板綍鏁�
			int page = Integer.parseInt(request.getParameter("page"));
			int rows = Integer.parseInt(request.getParameter("rows"));
			String name=request.getParameter("name");
			if(name==null){
				name="";
			}
			BD_Student student=new BD_Student();
			student.setName(name);
			try {
				EasyUIPager pager = service.getStudentList(rows, page,student);
				Gson gson=new Gson();
				response.getWriter().write(gson.toJson(pager));
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}else if(method.equals("remove")){
			
			try {
				String Id=request.getParameter("Id");
				service.deleteStudent(Id);
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}else if(method.equals("add")){
			String Name=request.getParameter("Name");
			String Gender=request.getParameter("Gender");
			String Birthday=request.getParameter("Birthday");
			String delBirthday=request.getParameter("delBirthday");
			String NativePlace=request.getParameter("NativePlace");
			String Tel=request.getParameter("Tel");
			String Email=request.getParameter("Email");
			String QQ=request.getParameter("QQ");
			String StudentID=request.getParameter("StudentID");
			String StartYear=request.getParameter("StartYear");
			String GraduationYear=request.getParameter("GraduationYear");
			String TypeTrain=request.getParameter("TypeTrain");
			String Instructor=request.getParameter("Instructor");
			String GraSource=request.getParameter("GraSource");
			String Cultivate=request.getParameter("Cultivate");
			String level=request.getParameter("level");
			String Graduation=request.getParameter("Graduation");
			String Job=request.getParameter("Job");
			BD_Student student=new BD_Student();
			student.setName(Name);
			student.setGender(Gender);
			student.setBirthday(Birthday);
			student.setNativePlace(NativePlace);
			student.setTel(Tel);
			student.setEmail(Email);
			student.setQQ(QQ);
			student.setStudentID(StudentID);
			student.setStartYear(StartYear);
			student.setGraduationYear( GraduationYear);
			student.setTypeTrain(TypeTrain);
			student.setInstructor(Instructor);
			student.setGraSource(GraSource);
			student.setCultivate(Cultivate);
			student.setlevel(level);
			student.setGraduation(Graduation);
			student.setJob(Job);
			System.out.println("add");
			System.out.println(Name);
			try {
				service.addStudent(student);
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}else if(method.equals("edit")){
			String Id=request.getParameter("Id");
			String Name=request.getParameter("Name");
			String Gender=request.getParameter("Gender");
			String Birthday=request.getParameter("Birthday");
			String delBirthday=request.getParameter("delBirthday");
			String NativePlace=request.getParameter("NativePlace");
			String Tel=request.getParameter("Tel");
			String Email=request.getParameter("Email");
			String QQ=request.getParameter("QQ");
			String StudentID=request.getParameter("StudentID");
			String StartYear=request.getParameter("StartYear");
			String GraduationYear=request.getParameter("GraduationYear");
			String TypeTrain=request.getParameter("TypeTrain");
			String Instructor=request.getParameter("Instructor");
			String GraSource=request.getParameter("GraSource");
			String Cultivate=request.getParameter("Cultivate");
			String level=request.getParameter("level");
			String Graduation=request.getParameter("Graduation");
			String Job=request.getParameter("Job");
			BD_Student student=new BD_Student();
			student.setName(Name);
			student.setGender(Gender);
			student.setBirthday(Birthday);
			student.setNativePlace(NativePlace);
			student.setTel(Tel);
			student.setEmail(Email);
			student.setQQ(QQ);
			student.setStudentID(StudentID);
			student.setStartYear(StartYear);
			student.setGraduationYear( GraduationYear);
			student.setTypeTrain(TypeTrain);
			student.setInstructor(Instructor);
			student.setGraSource(GraSource);
			student.setCultivate(Cultivate);
			student.setlevel(level);
			student.setGraduation(Graduation);
			student.setJob(Job);
			System.out.println("edit");
			System.out.println(Name);
			try {
				service.editStudent(student, Id);
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
	}

	

}
